<template>
<div class="el-card cardBorder">
  <div class="operate">
    <el-button type="primary" @click="addUser">添加</el-button>
    <el-input style="width: 30%;margin: 0 5px" v-model="searchParams.userName" placeholder="请输入用户名"></el-input>
    <el-select v-model="searchParams.sex" placeholder="请选择用户性别" style="margin: 0 5px">
      <el-option key="1" label="男" value="0"></el-option>
      <el-option key="2" label="女" value="1"></el-option>
    </el-select>
    <el-select v-model="searchParams.userType" placeholder="请选择用户类型" style="margin: 0 5px">
      <el-option key="1" label="管理员" value="0"></el-option>
      <el-option key="2" label="教师" value="1"></el-option>
      <el-option key="3" label="用户" value="2"></el-option>
    </el-select>
    <el-select v-model="searchParams.status" placeholder="请选择用户状态" style="margin: 0 5px">
      <el-option key="1" label="正常" value="0"></el-option>
      <el-option key="2" label="停用" value="1"></el-option>
    </el-select>
    <el-button type="primary" icon="el-icon-search" @click="getAllUser">搜索</el-button>
  </div>
  <el-table
      :data="users"
      border
      style="width: 100%;height: 100%;margin-bottom: 10px">
    <el-table-column prop="id" label="id"></el-table-column>
    <el-table-column prop="userName" label="用户名"></el-table-column>
    <el-table-column prop="nickName" label="姓名"></el-table-column>
    <el-table-column prop="phonenumber" label="手机号"></el-table-column>
    <el-table-column  label="身份">
      <template v-slot="scope">
        <el-tag v-if="scope.row.userType==='0'" type="danger">管理员</el-tag>
        <el-tag v-if="scope.row.userType==='1'" type="success">教师</el-tag>
        <el-tag v-if="scope.row.userType==='2'">学生</el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="sex" label="性别">
      <template slot-scope="scope">
        <span v-if="scope.row.sex==='0'">男</span>
        <span v-if="scope.row.sex==='1'">女</span>
      </template>
    </el-table-column>
    <el-table-column prop="status" label="账号状态">
      <template slot-scope="scope">
        <el-tag v-if="scope.row.status==='0'" type="success">有效</el-tag>
        <el-tag v-if="scope.row.status==='1'" type="danger">停用</el-tag>
      </template>
    </el-table-column>
    <el-table-column  label="操作">
      <template slot-scope="scope">
        <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)" v-if="scope.row.userType==='0'" disabled>不可编辑</el-button>
        <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)" v-if="scope.row.userType!=='0'">编辑</el-button>
        <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"  v-if="scope.row.userType!=='0'">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="searchParams.pageNum"
      :page-size="searchParams.pageSize"
      :page-sizes="[5, 8, 10, 20]"
      layout="total, prev, pager, next"
      :total="total">
  </el-pagination>
  <user-dialog :visible.sync="addDialog" title="添加用户" @addResult="getAllUser"></user-dialog>
  <user-update-dialog :visible.sync="updateDialog" :user="this.updateUser" title="修改用户" @addResult="getAllUser"></user-update-dialog>
</div>
</template>

<script>

import {deleteUser, getUserByPage} from "@/api/user";
import UserDialog from "@/components/dialog/userDialog";
import UserUpdateDialog from "@/components/dialog/userUpdateDialog";

export default {
  name: "index",
  components: {UserUpdateDialog, UserDialog},
  data(){
    return{
      searchParams:{
        pageNum: 1,
        pageSize: 4,
        userName:'',
        userType:'',
        sex:'',
        status:''
      },
      users:[],
      total:10,
      addDialog:false,
      updateUser:{},
      updateDialog:false
    }
  },
  methods:{
    getAllUser(){
      getUserByPage(this.searchParams).then(res=>{
        if (res.code===200){
          this.$message.success("用户数据获取成功")
          this.users=res.data.records;
          this.total=res.data.total;
        }
      })
    },
    handleSizeChange(val){
        this.getAllUser();
    },
    handleCurrentChange(val){
        this.getAllUser();
    },
    handleEdit(index,row){
      this.updateUser=JSON.parse(JSON.stringify(row));
      this.updateUser.status=(this.updateUser.status==='0')?true:false
      this.updateDialog=true;
    },
    handleDelete(index,row){
      deleteUser(row.id).then(res=>{
        if (res.code===200){
          this.$message.success("成功删除")
          this.getAllUser();
        }
      })
    },
    addUser(){
      this.addDialog=true;
    }
  },
  mounted() {
    this.getAllUser();
  }
}
</script>

<style lang="less" scoped>
.cardBorder{
  width: 96%;
  padding: 20px;
  .operate{
     margin: 16px;
  }
}
</style>
